﻿using System;
using Missbot.Common;
using Missbot.Service;

namespace Missbot.AIModule.Vip
{
    public class DailyVipGoodsRecord : DbBaseEntity
    {
        public string UserID { get; init; }

        public string Date { get; set; }

        public string[] GoodsName { get; set; }
    }

    public static class DailyVipGoodsDA
    {
        public static DailyVipGoodsRecord GetToday(string UserID)
        {
            var record = MongoService<DailyVipGoodsRecord>.GetOnly(p => p.UserID == UserID);
            if (record == null)
            {
                record = new DailyVipGoodsRecord { UserID = UserID };
                MongoService<DailyVipGoodsRecord>.Insert(record);
            }

            var dateStr = DateTime.Now.ToString("yyyy-MM-dd");
            if (record.Date == dateStr)
            {
                return record;
            }

            record.Date = dateStr;
            record.GoodsName = AutofacSvc.Resolve<DailyVipShopSvc>().RandGoods(7);
            record.Update();

            return record;
        }

        public static void Update(this DailyVipGoodsRecord entity)
        {
            MongoService<DailyVipGoodsRecord>.Update(entity);
        }

        public static void Refresh(string UserID)
        {
            var rec = GetToday(UserID);
            MongoService<DailyVipGoodsRecord>.Delete(rec);
        }
    }
}